package com.bs.teachassistant.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bs.teachassistant.R;
import com.bs.teachassistant.activity.CheckClassActivity;
import com.bs.teachassistant.activity.LoginActivity;
import com.bs.teachassistant.activity.ShareActivity;
import com.bs.teachassistant.entity.User;
import com.bs.teachassistant.utils.FormatUtils;
import com.bs.teachassistant.utils.GsonUtils;
import com.bs.teachassistant.view.CircleTransform;
import com.bs.teachassistant.view.MessageDialog;
import com.squareup.picasso.Picasso;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

/**
 * Created by limh on 2020/4/24.
 * 设置页面
 */
@ContentView(R.layout.fragment_mine)
public class MineFragment extends BaseFragment {
    @ViewInject(R.id.image_user_header)
    ImageView imageHeader;
    @ViewInject(R.id.txt_user_name)
    TextView txtName;
    User user;

    private MessageDialog dialog;

    @Override
    public void initViews() {
        if (!TextUtils.isEmpty(userPreference.getString("useInfo", ""))) {
            user = GsonUtils.GsonToBean(userPreference.getString("useInfo", ""), User.class);
            if (user.getSex().equals("boy")) {
                Picasso.with(getContext()).load(R.drawable.default_boy).
                        transform(new CircleTransform()).into(imageHeader);
            } else {
                Picasso.with(getContext()).load(R.drawable.default_girl).
                        transform(new CircleTransform()).into(imageHeader);
            }
            txtName.setText(user.getUserName());
        } else {
            Picasso.with(getContext()).load(R.drawable.default_boy).
                    transform(new CircleTransform()).into(imageHeader);
        }
    }

    @Override
    public void initDatas() {

    }

    @Event(value = {R.id.txt_user_name, R.id.txt_collec,R.id.txt_record,
            R.id.txt_about, R.id.txt_exit, R.id.txt_tucao}, type = View.OnClickListener.class)
    private void OnClick(View view) {
        switch (view.getId()) {
            case R.id.txt_user_name:
                if (null != user) {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                    getActivity().overridePendingTransition(R.anim.activity_enter, R.anim.activity_exit);
                }
                break;
            case R.id.txt_collec:
                Intent intent = new Intent();
                intent.setClass(getActivity(), ShareActivity.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.activity_enter, R.anim.activity_exit);
                break;
            case R.id.txt_record:
                Intent intent1 = new Intent();
                intent1.setClass(getActivity(), CheckClassActivity.class);
                startActivity(intent1);
                getActivity().overridePendingTransition(R.anim.activity_enter, R.anim.activity_exit);
                break;

            case R.id.txt_tucao:
                showMsgDialog("提示", "请上线后再来吐槽");
                break;
            case R.id.txt_about:
                showMsgDialog("关于App", "高校教师助手" + " V" + FormatUtils.getVersionName(getContext()) + "\n"
                        + "XXX大学：XXX毕业设计");
                break;
            case R.id.txt_exit:
                showExitDialog();
                break;
        }
    }

    /**
     * 展示信息提示对话框
     */
    private void showExitDialog() {
        if (null == dialog)
            dialog = MessageDialog.getInstence(getActivity());
        dialog.setCustomDialog("提示", "您确定要退出吗？", "取消", "确定");
        dialog.setOnNegativeListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialog.isShowing())
                    dialog.dismiss();
            }
        });
        dialog.setOnPositiveListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
                Intent intent=new Intent();
                intent.setClass(getActivity(),LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });
        if (!dialog.isShowing())
            dialog.show();
    }

    /**
     * 退出提示对话框
     *
     * @param title 对话框标题
     * @param msg   对话框信息内容
     */
    private void showMsgDialog(String title, String msg) {
        if (null == dialog)
            dialog = MessageDialog.getInstence(getActivity());
        dialog.setCustomDialog(title, msg, "", "确定");
        dialog.setOnNegativeListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialog.isShowing())
                    dialog.dismiss();
            }
        });
        dialog.setOnPositiveListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialog.isShowing())
                    dialog.dismiss();
            }
        });
        if (!dialog.isShowing())
            dialog.show();
    }
}
